package com.sun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.dao.MenuMapper;
import com.sun.pojo.Menu;
import com.sun.service.MenuService;
import com.sun.utils.DataGridViewResult;
import com.sun.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuMapper menuMapper;


    public List<Menu> findAllMenu() {
        return menuMapper.findAllMenu();
    }

    public List<Integer> findMenuListByRoleId(int roleId) {
        return menuMapper.findMenuListByRoleId(roleId);
    }

    public DataGridViewResult findMenuByPage(MenuVo menuVo) {
        PageHelper.startPage(menuVo.getPage(), menuVo.getLimit());
        List<Menu> menuByPage = menuMapper.findMenuByPage(menuVo);
        PageInfo<Menu> pageInfo = new PageInfo<Menu>(menuByPage);
        return new DataGridViewResult(pageInfo.getTotal(), pageInfo.getList());
    }

    public int addMenu(Menu menu) {
        // 如果父菜单未选择 则默认设置为0 顶级菜单
        System.out.println(menu.getPid());
        if(menu.getPid()==null){
            menu.setPid(0);
        }
        menu.setIcon("fa "+menu.getIcon());
        menu.setTarget("_self");
        return menuMapper.addMenu(menu);
    }

    public int updateMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    public int checkMenuExistChild(int id) {
        return menuMapper.checkMenuExistChild(id);
    }

    public int deleteMenu(int id) {
        return menuMapper.deleteMenu(id);
    }

    public List<Menu> findAllMenuByUserId(int id) {
        return menuMapper.findAllMenuByUserId(id);
    }
}
